<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/9/2
 * Time: 10:46
 */
namespace Admin\Controller;
use Think\Controller;
class MemberController extends Controller{

    public function index(){

        $key = '';

        $where = '';

        if( I('keyword') ){

            $where = "a.email like '%".I('keyword')."%' ";

            $key = I('keyword');

        }

        $User = M('member');

        $count = $User->count();

        $Page = new \Think\Page($count,C('limitPage'));

        $show = $Page->show();

        $list = $User->alias('a')->field('a.id,a.email,a.nickname,a.logintime,a.loginip,a.islock,b.name')->join("xyh_membergroup as b on a.groupid = b.id",'left')->where("$where")->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);

        $this->assign('key',$key);

        $this->assign('count',$count);

        $this->assign('page',$show);

        $this->display();

    }

    public function add(){

        $User = M('membergroup');

        $list = $User->select();

        $this->assign('list',$list);

        $this->display();

    }

    public function addUser(){

        $num = I('post.');

        $User = M('member');

        $email = "/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/";

        if (preg_match($email ,$num['email'])){

            $User->add($num);

            $this->success('添加成功','index',3);

        }else{

            echo "<script>alert('邮箱格式不正确！');history.back();</script>";

        }

    }

    public function del(){

        $User = M('member');

        $id = is_array(I('id')) ? implode(',', I('id')) : I('id');

        if ( $User->delete($id) > 0 ){

            echo 1;

        }else{

            echo 0;

        }

    }

    public function edit(){

        $id = I('id');

        $User = M('member');

        $list = $User->find($id);

        $this->assign('arr',$list);

        $Users = M('membergroup');

        $lists = $Users->select();

        $this->assign('list',$lists);

        $this->display();

    }

    public function editUser(){

        $num = I('post.');

        $User = M('member');

        $email = "/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/";

        $pwd = "/^.+$/";

        if (preg_match($email ,$num['email'])){

            if (preg_match($pwd ,$num['password'])){

                $num['password'] = md5($num['password']);

                $User->save($num);

                $this->success('修改成功','index',3);

            }else{

                echo "<script>alert('密码不能为空！');history.back();</script>";

            }

        }else{

            echo "<script>alert('邮箱格式不正确！');history.back();</script>";

        }

    }

    public function person(){

        $id = I('id');

        $User = M('member');

        $list = $User->find($id);

        $this->assign('arr',$list);

        $this->display();

    }

}